﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Client.TopLevel;

namespace PersApp
{
    public partial class UserDataForm : Form
    {
        //members
        private IManagerEmployee m_manager;
        private TLEmployee m_employee;
        private TLReport m_report;
        private TLPosition m_position;

        public UserDataForm(TLEmployee employee, TLReport report, TLPosition position, IManagerEmployee manager)
        {
            InitializeComponent();

            m_employee = employee;
            m_report = report;
            m_position = position;
            m_manager = manager;

            //заполняем поля карточки пользователя
            textBoxLogin.Text = employee.Login;
            textBoxName.Text = employee.Name;
            textBoxPosition.Text = position.Name;
            textBoxSalary.Text = position.Salary.ToString();

            List<TLWorkTime> timetables = report.Time;
            dataGridView1.RowCount = 0;
            DateTime timeIn, timeOut; 
            foreach (TLWorkTime workTime in timetables)
            {
                ++dataGridView1.RowCount;
                if (Convert.ToDateTime(workTime.TimeOut).Year > 1)
                {
                    timeIn = Convert.ToDateTime(workTime.TimeIn).AddHours(2.0);
                    timeOut = Convert.ToDateTime(workTime.TimeOut).AddHours(2.0);
                    dataGridView1.Rows[dataGridView1.RowCount - 1].SetValues(timeIn, timeOut, timeOut-timeIn);
                }
                else
                {
                    timeIn = Convert.ToDateTime(workTime.TimeIn).AddHours(2.0);
                    dataGridView1.Rows[dataGridView1.RowCount - 1].SetValues(timeIn, "...", "...");
                }
            }

            ShowSalary();
        }

        private void ShowSalary()
        {
            DateTime now = DateTime.Now;
            DateTime prev = new DateTime(now.Year, now.Month, 1);
            prev.AddDays(-prev.Day);
            for (int i = 0; i < 12; i++)
            {
                ++dataGridView2.RowCount;
                dataGridView2.Rows[dataGridView2.RowCount-1].SetValues(IntToMonth(prev.Month), 
                    m_manager.GetSalary(prev, now));
                now = prev;
                if (now.Month != 1)
                    prev = new DateTime(now.Year, now.Month - 1, 1);
                else prev = new DateTime(now.Year - 1, 12, 1);
            }
        }

        private string IntToMonth(int m)
        {
            switch (m % 12)
            {
                case 1: return "Январь";
                case 2: return "Февраль";
                case 3: return "Март";
                case 4: return "Апрель";
                case 5: return "Май";
                case 6: return "Июнь";
                case 7: return "Июль";
                case 8: return "Август";
                case 9: return "Сентябрь";
                case 10: return "Октябрь";
                case 11: return "Ноябрь";
                default: return "Декабрь";
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
